<template>
  <div class="app-container">
    <div>
      <el-form ref="form" :model="queryParams" inline>
        <el-form-item label="表名" prop="tableName">
          <el-input v-model="queryParams.tableName" size="small"/>
        </el-form-item>
        <el-form-item label="表名">
          <el-button type="primary" @click="getList" size="small">查询</el-button>
          <el-button type="primary" @click="resetForm('form')" size="small">重置</el-button>
          <el-button type="danger" @click="generator" size="small">生成</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div>
      <el-table v-loading="tableLoading"
                :data="tableData"
                height="600"
                @selection-change="getSelection"
                border>
        <el-table-column type="selection"></el-table-column>
        <el-table-column label="表名" prop="name"></el-table-column>
        <el-table-column label="注释" prop="comment"></el-table-column>
        <el-table-column label="存储引擎" prop="engine"></el-table-column>
        <el-table-column label="创建时间" prop="createTime"></el-table-column>
        <el-table-column label="编码" prop="encode"></el-table-column>
      </el-table>
      <Pagination
        background
        :page-sizes="pageWrapper.pageSizes"
        :total="pageWrapper.total"
        :limit.sync="pageWrapper.size"
        :page.sync="pageWrapper.current"
        auto-scroll
        @pagination="getList"
        layout="sizes,prev, pager, next"
      />
    </div>
  </div>
</template>

<script>
import Pagination from '@/components/Pagination'
import {listData,} from "@/api/system/gen";
import {pageMixin,formMixin} from "@/mixins/mixins";
import {axiosDownload} from "@/utils/axios-download";


export default {
  mixins:[pageMixin,formMixin],
  name: "generator",
  components: {Pagination},
  data() {
    return {
      tableLoading:false,
      tableData: [],
      selectData:[],
      queryParams:{
        tableName:''
      }
    }
  },
  mounted() {
    this.getList();
  },
  methods: {
    getList() {
      this.tableLoading = true;
      listData({current: this.pageWrapper.current, size: this.pageWrapper.size,...this.queryParams})
      .then(res => {
        this.setPageWrapper(res.data);
        this.tableLoading = false;
        this.tableData = res.data.records;
      })
    },
    getSelection(selection){
      this.selectData = selection;
    },
    generator(){
      //获取选择的表 生成
      if(this.selectData.length==0){
        this.$notify['warning']({
          title:'未选择任何表'
        })
        return;
      }

      this.$prompt('输入模块包名', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        inputPattern: /\w/,
        inputErrorMessage: '必填'
      }).then(({ value }) => {
        axiosDownload({
          url:'/generator/gen',
          method:'post',
          data:{
            packagePath:value,
            tableNames:this.selectData.map(item =>item.name)
          },
          type:'zip'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '取消输入'
        });
      });


    }

  }
}
</script>

<style scoped>

</style>
